home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / stdio / c / flsbuf < prev    next >
Text File  |  1996-11-09  |  2KB  |  77 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/flsbuf,v $
  4.  * $Date: 1996/05/06 09:01:34 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: flsbuf,v $
  10.  * Revision 1.2  1996/05/06 09:01:34  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:32:42  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: flsbuf,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25.  
  26. __STDIOLIB__
  27.  
  28. /* __flsbuf(-1,f) flushes output without adding c */
  29.  
  30. int
  31. __flsbuf (register int c, register FILE * f)
  32. {
  33.   register int n, b, g = f->flag;
  34.   register unsigned char *a, *p;
  35.  
  36.   if ((g & (_IOWRITE | _IOERR)) != _IOWRITE)
  37.     return (-1);
  38.  
  39.   b = (g & _IONBF) ? 1 : f->bufsiz;
  40.  
  41.   if (!(a = f->o_base))
  42.     {
  43.       if (!(a = f->o_base = malloc (b)))
  44.     {
  45.       f->flag = g | _IOERR;
  46.       return (-1);
  47.     }
  48.       if (b > 1 && c >= 0 && !(c == '\n' && (g & _IOLBF)))
  49.     {
  50.       f->o_cnt = b - 1;
  51.       f->o_ptr = a + 1;
  52.       return (*a = c);
  53.     }
  54.       p = f->o_ptr = a;
  55.     }
  56.   else
  57.     p = f->o_ptr;
  58.  
  59.   if (c >= 0)
  60.     *p++ = c;
  61.  
  62.   n = p - a;
  63.  
  64.   if (n > 0)
  65.     if (write (f->fd, a, n) < n)
  66.       {
  67.     f->flag = g | _IOERR;
  68.     return (-1);
  69.       }
  70.  
  71.   f->pos += n;
  72.   f->o_cnt = b;
  73.   f->o_ptr = a;
  74.  
  75.   return ((c >= 0) ? c : 0);
  76. }
  77.